      SUBROUTINE DOSODIIS2_D2(ITER, VECINOUT, LENGTH, MAXDIM, SCR, MAXCOR,
     &                    RESMDIIS, IDOSO, ITMP)
!
      IMPLICIT NONE
      INTEGER, intent(in) :: ITER, LENGTH, MAXDIM
      INTEGER*8, intent(in) :: MAXCOR
!
      DOUBLE PRECISION, intent(inout) :: IDOSO(*), ITMP(*)
      DOUBLE PRECISION, intent(inout) :: RESMDIIS
      DOUBLE PRECISION, intent(inout) :: SCR(MAXCOR), VECINOUT(LENGTH)
!
      INTEGER :: LENVEC, NDIMR, IDIMR
      COMMON/DIISDAT1/LENVEC,NDIMR,IDIMR
!
      INTEGER :: I, J, K, N, I0, I11, I12, I2, IEND, MAXDIM1, INDXF
      INTEGER :: IOFF(20)
      DOUBLE PRECISION :: FACT, RESMITER, X
!
      INTEGER, external :: IDAMAX
      DOUBLE PRECISION, external :: DDOT
      DOUBLE PRECISION, external :: DNRM2, NRM2SO
!
      INDXF(I,J,N)=I+(J-1)*N
!
      IOFF(1) = 1
      do I = 1, ndimr+maxdim -1
         ioff(i+1) = ioff(i) + LENGTH
      enddo
!
      MAXDIM1 = MAXDIM + 2
      LENVEC = LENGTH
!
      I0  = 1
      I11 = I0  + MAXDIM1*MAXDIM1
      I12 = I11 + MAXDIM1*MAXDIM1
      I2  = I12 + MAXDIM1
      IEND = I2 + LENVEC
      RESMDIIS = 1.D0
!      write(6,*)'I in dosodiis', i11, i12, i2, iend
!     
      IF (MAXCOR<IEND) THEN
         WRITE(6,*) 'Insufficient memory in DOSODIIS'
         STOP
      ENDIF
!
!      write(6,*)'begin dosodiis'
      IF (ITER==1) THEN
!         call clmoiodiis(scr, lenvec, 1, maxdim*2, 'DIISTMP2', 4096)
!         OPEN(20, FILE='DIIS.tmp', STATUS='UNKNOWN', ACCESS='DIRECT',
!     &        FORM='UNFORMATTED', RECL=8*LENGTH)
         IDIMR = 1 
         NDIMR = 1
!        OPEN(21, FILE='TMPAMPTUDE', STATUS='UNKNOWN',
!    &        FORM='UNFORMATTED')
!        REWIND 21
!        WRITE(21) (VECINOUT(I), I = 1, LENVEC)
         ITMP(1:LENVEC)=VECINOUT(1:LENVEC)
         RETURN
      ENDIF
!
!      REWIND 21
!      READ(21) (SCR(I), I = 1, LENVEC)
      call myicopyso(itmp, scr, lenvec) 
!      write(6,*)'begin dosodiis'
!      write(6,*)'rec and ndimr',idimr+maxdim, ndimr
!      call putdiis(scr, 1, 1, idimr+maxdim, 'DIISTMP2')
      call myicopyso(ITMP, IDOSO(IOFF(idimr+maxdim)), LENVEC)
     
!
!      IF(NDIMR==1) WRITE(21) (VECINOUT(I), I = 1, LENVEC)
      IF(NDIMR==1) ITMP(1:LENVEC) = VECINOUT(1:LENVEC)
!
!      write(6,*)'begin dosodiis'
!      CALL DAXPY(LENVEC,-1.D0,SCR(1),1,VECINOUT,1)
      CALL DAXPY(LENVEC,-1.D0,SCR(1),1,VECINOUT,1)
!      call putdiis(vecinout, 1, 1, idimr, 'DIISTMP2')
      CALL MYICOPYSO(VECINOUT, IDOSO(IOFF(IDIMR)), LENVEC)
      RESMITER = VECINOUT(IDAMAX(LENVEC, VECINOUT, 1))
!      write(6,*)'begin dosodiis'
      DO I = 1, NDIMR
         call myicopyso(idoso(ioff(i)), scr(i2), LENVEC)
!         call getdiis(scr(i2),1, 1, i, 'DIISTMP2')
         DO J = 1, I
          call myicopyso(idoso(ioff(j)), vecinout, LENVEC)
!         call getdiis(vecinout,1, 1, J, 'DIISTMP2')
            X = DDOT(LENVEC, SCR(I2), 1, VECINOUT, 1)
            SCR(INDXF(I,J,MAXDIM))=X
            SCR(INDXF(J,I,MAXDIM))=X
         ENDDO
      ENDDO
!
      CALL DODIIS(SCR, SCR(I11), SCR(I12), MAXDIM, NDIMR)
!
      VECINOUT = 0.D0
      DO I = 1, NDIMR
         FACT = SCR(I12+I-1)
!        WRITE(6,*) 'I COEF', I, FACT
!         READ(20, REC=I) (SCR(K), K = I2, IEND-1)
!         call getdiis(scr(I2),1, 1, I, 'DIISTMP2')
         call myicopyso(idoso(ioff(i)), scr(i2), LENVEC)
         CALL DAXPY(LENVEC, FACT, SCR(I2), 1, VECINOUT, 1)
      ENDDO
      RESMDIIS = VECINOUT(IDAMAX(LENVEC, VECINOUT, 1))
!      write(6,*)'norm of vecinout 5', nrm2so(lenvec, vecinout, 1)
!
      WRITE(6,1001) ITER
      WRITE(6,1002) RESMITER
      WRITE(6,1003) RESMDIIS
!
      DO I = 1, NDIMR
         FACT = SCR(I12+I-1)
!         call getdiis(scr(I2),1, 1, I+maxdim, 'DIISTMP2')
         call myicopyso(idoso(ioff(i+maxdim)), scr(i2), LENVEC)
!         READ(20, REC=I+MAXDIM) (SCR(K), K = I2, IEND-1)
         CALL DAXPY(LENVEC, FACT, SCR(I2), 1, VECINOUT, 1)
      ENDDO
!      write(6,*)'norm of vecinout 6', nrm2so(lenvec, vecinout, 1)
!
!      REWIND 21
      call myicopyso(vecinout, itmp, lenvec)
!      WRITE(21) (VECINOUT(I), I = 1, LENVEC)
!      write(6,*)'norm of vecinout 7', nrm2so(lenvec, vecinout, 1)
!
      NDIMR=MIN(MAXDIM,NDIMR+1)
      IDIMR=IDIMR+1
      IF(IDIMR.GT.MAXDIM) IDIMR=1
!      write(6,*)'-------------DOSODIIS_D2----------------'
!      write(6,*)'LENGTH', LENGTH
!      write(6,*)'MAXDIM', MAXDIM
!      write(6,*)'MAXCOR', MAXCOR
!      write(6,*)'LENVEC', LENVEC
!      write(6,*)'NDIMR ', NDIMR
!      write(6,*)'IDIMR ', IDIMR
!      write(6,*)'----------------------------------------'
!      write(6,*)'data in doso', LENVEC, NDIMR, IDIMR
!
!      write(6,*)'dosodiis', idimr, maxdim, ndimr
1001  FORMAT(T3,'Convergence information after ',I5,' iterations: ')
1002  FORMAT(T3,'Largest element of residual vector : ',E15.8,'.')
1003  FORMAT(T3,'Largest element of DIIS residual   : ',E15.8,'.')
!
      RETURN
      END      

